import requestInstance from '@/utils/request'
/**
 * 分析页面SEO
 * @param {string} url - 页面URL
 * @param {string} content - 页面内容
 * @returns {Promise} 分析结果
 */
export const analyzePageSeo = async (url, content) => {
  try {
    const response = await requestInstance.post('seo/analyze', {
      url,
      content
    });
    return response.data;
  } catch (error) {
    console.error('SEO分析失败:', error);
    throw error;
  }
};

/**
 * 获取高级SEO设置
 * @returns {Promise} SEO设置
 */
export const getAdvancedSeoSettings = async () => {
  try {
    const response = await requestInstance.get('seo/settings');
    return response.data;
  } catch (error) {
    console.error('获取SEO设置失败:', error);
    throw error;
  }
};

/**
 * 保存高级SEO设置
 * @param {Object} settings - SEO设置
 * @returns {Promise} 保存结果
 */
export const saveAdvancedSeoSettings = async (settings) => {
  try {
    const response = await requestInstance.post('seo/settings', settings);
    return response.data;
  } catch (error) {
    console.error('保存SEO设置失败:', error);
    throw error;
  }
};

/**
 * 重新生成网站地图
 * @returns {Promise} 生成结果
 */
export const regenerateSitemap = async () => {
  try {
    const response = await requestInstance.post('seo/sitemap/generate');
    return response.data;
  } catch (error) {
    console.error('生成网站地图失败:', error);
    throw error;
  }
};

/**
 * 获取SEO统计数据
 * @param {Object} params - 查询参数
 * @returns {Promise} 统计数据
 */
export const getSeoStats = async (params = {}) => {
  try {
    const response = await requestInstance.get('seo/stats', {
      params
    });
    return response.data;
  } catch (error) {
    console.error('获取SEO统计数据失败:', error);
    throw error;
  }
};

/**
 * 获取站点SEO健康状态
 * @returns {Promise} 健康状态
 */
export const getSeoHealth = async () => {
  try {
    const response = await requestInstance.get('seo/health');
    return response.data;
  } catch (error) {
    console.error('获取SEO健康状态失败:', error);
    throw error;
  }
};

/**
 * 获取热门关键词数据
 * @param {number} limit - 限制数量
 * @returns {Promise} 关键词数据
 */
export const getPopularKeywords = async (limit = 20) => {
  try {
    const response = await requestInstance.get('seo/keywords/popular', {
      params: { limit }
    });
    return response.data;
  } catch (error) {
    console.error('获取热门关键词失败:', error);
    throw error;
  }
};

/**
 * 获取基础SEO设置
 * @returns {Promise} SEO设置
 */
export const getSeoSettings = async () => {
  try {
    const response = await requestInstance.get('seo/basic-settings');
    return response.data;
  } catch (error) {
    console.error('获取基础SEO设置失败:', error);
    throw error;
  }
};

/**
 * 保存基础SEO设置
 * @param {Object} settings - SEO设置
 * @returns {Promise} 保存结果
 */
export const saveSeoSettings = async (settings) => {
  try {
    const response = await requestInstance.post('seo/basic-settings', settings);
    return response.data;
  } catch (error) {
    console.error('保存基础SEO设置失败:', error);
    throw error;
  }
};

/**
 * 添加监控关键词
 * @param {Object} keywordData - 关键词数据
 * @returns {Promise} 添加结果
 */
export const addKeyword = async (keywordData) => {
  try {
    const response = await requestInstance.post('seo/keywords/monitor', keywordData);
    return response.data;
  } catch (error) {
    console.error('添加监控关键词失败:', error);
    throw error;
  }
};

/**
 * 获取监控关键词列表
 * @param {Object} params - 查询参数
 * @returns {Promise} 关键词列表
 */
export const getKeywords = async (params = {}) => {
  try {
    const response = await requestInstance.get('seo/keywords/monitor', {
      params
    });
    return response.data;
  } catch (error) {
    console.error('获取关键词列表失败:', error);
    throw error;
  }
};

/**
 * 更新监控关键词
 * @param {string|number} id - 关键词ID
 * @param {Object} keywordData - 更新的关键词数据
 * @returns {Promise} 更新结果
 */
export const updateKeyword = async (id, keywordData) => {
  try {
    const response = await requestInstance.put(`/api/seo/keywords/monitor/${id}`, keywordData);
    return response.data;
  } catch (error) {
    console.error('更新关键词失败:', error);
    throw error;
  }
};

/**
 * 删除监控关键词
 * @param {string|number} id - 关键词ID
 * @returns {Promise} 删除结果
 */
export const deleteKeyword = async (id) => {
  try {
    const response = await requestInstance.delete(`/api/seo/keywords/monitor/${id}`);
    return response.data;
  } catch (error) {
    console.error('删除关键词失败:', error);
    throw error;
  }
};

/**
 * 获取关键词统计数据
 * @returns {Promise} 统计数据
 */
export const getKeywordStats = async () => {
  try {
    const response = await requestInstance.get('seo/keywords/stats');
    return response.data;
  } catch (error) {
    console.error('获取关键词统计数据失败:', error);
    throw error;
  }
};

/**
 * 获取流量趋势数据
 * @param {Object} params - 查询参数
 * @returns {Promise} 流量趋势数据
 */
export const getTrafficTrend = async (params = {}) => {
  try {
    const response = await requestInstance.get('seo/traffic/trend', {
      params
    });
    return response.data;
  } catch (error) {
    console.error('获取流量趋势数据失败:', error);
    throw error;
  }
};

/**
 * 获取排名趋势数据
 * @param {Object} params - 查询参数
 * @returns {Promise} 排名趋势数据
 */
export const getRankTrend = async (params = {}) => {
  try {
    const response = await requestInstance.get('seo/rank/trend', {
      params
    });
    return response.data;
  } catch (error) {
    console.error('获取排名趋势数据失败:', error);
    throw error;
  }
};

/**
 * 获取关键词分布数据
 * @param {Object} params - 查询参数
 * @returns {Promise} 关键词分布数据
 */
export const getKeywordDistribution = async (params = {}) => {
  try {
    const response = await requestInstance.get('seo/keywords/distribution', {
      params
    });
    return response.data;
  } catch (error) {
    console.error('获取关键词分布数据失败:', error);
    throw error;
  }
};

/**
 * 获取搜索引擎分布数据
 * @param {Object} params - 查询参数
 * @returns {Promise} 搜索引擎分布数据
 */
export const getSearchEngineDistribution = async (params = {}) => {
  try {
    const response = await requestInstance.get('seo/search-engines/distribution', {
      params
    });
    return response.data;
  } catch (error) {
    console.error('获取搜索引擎分布数据失败:', error);
    throw error;
  }
};

/**
 * 获取热门关键词列表
 * @param {Object} params - 查询参数
 * @returns {Promise} 热门关键词列表
 */
export const getTopKeywords = async (params = {}) => {
  try {
    const response = await requestInstance.get('seo/keywords/top', {
      params
    });
    return response.data;
  } catch (error) {
    console.error('获取热门关键词列表失败:', error);
    throw error;
  }
};

/**
 * 获取热门页面列表
 * @param {Object} params - 查询参数
 * @returns {Promise} 热门页面列表
 */
export const getTopPages = async (params = {}) => {
  try {
    const response = await requestInstance.get('seo/pages/top', {
      params
    });
    return response.data;
  } catch (error) {
    console.error('获取热门页面列表失败:', error);
    throw error;
  }
};